package de.hsb.ai.agent.misterstar;

import de.hsb.ai.util.map.ScotlandJadeMapHelper.ScotlandJadeVertex;

public class Node {
	
	private ScotlandJadeVertex node = null;
	private Node parent = null;
	private Node bestChild = null;
	private Node nextBestChild = null;
	
	private double 	h = -1;
	private double 	g = 0;
	
	public Node(Node parent, ScotlandJadeVertex vertex, double heuristic){
		this.parent = parent;
		this.h = heuristic;
		node = vertex;
	}
	
	public ScotlandJadeVertex getNode() {
		return node;
	}
	public void setNode(ScotlandJadeVertex node) {
		this.node = node;
	}
	public Node getParent() {
		return parent;
	}
	public void setParent(Node parent) {
		this.parent = parent;
	}
	public Node getBestChild() {
		return bestChild;
	}
	public void setBestChild(Node bestChild) {
		this.bestChild = bestChild;
	}
	public Node getNextBestChild() {
		return nextBestChild;
	}
	public void setNextBestChild(Node nextBestChild) {
		this.nextBestChild = nextBestChild;
	}
	public double getH() {
		return h;
	}
	public void setH(double h) {
		this.h = h;
	}
	
	public double getG() {
		return g;
	}
	public void setG(double g) {
		this.g = g;
	}
	
	public void switchChild() {
		Node temp = bestChild;
		bestChild = nextBestChild;
		nextBestChild = temp;
		 
	}
	
	public double getHeuristic() {
		return g+h;
	}
	
	public int getIndex() {
		return node.getIndex();
	}
	public String toString() {
		String output="";
		Node node = this;
		while(node != null)
		{
			output += "[ ";
			
			if(node.getNode() != null)
				output += node.getNode().getIndex();
			else
				output += " null ";
			
			output += " -> ";
			if(node.getBestChild() != null )
			{
				output += node.getBestChild().getIndex() + "("+Math.round(node.getBestChild().getHeuristic()) + "), ";
			}
			else
			{
				output += "null(null), ";
			}
			
			if(node.getNextBestChild() != null  )
			{
				output += node.getNextBestChild().getIndex() + "(" +Math.round(node.getNextBestChild().getHeuristic()) + ")";
			}
			else
			{
				output += "null(null)";
			}
			
			output += "]\n";
			node = node.getParent();
		}
		
		return output;
	}
	
	public int countParents()
	{
		int count=0;
		Node node = this;
		while(node.getParent() != null)
		{
			count++;
			node = node.getParent();
		}
		
		return count;
	}
	

}
